package com.oig.sys.file.controller;

import com.oig.common.module.SysResponse;
import com.oig.common.validator.GroupAdd;
import com.oig.common.validator.GroupEdit;
import com.oig.sys.file.dto.TemplateFileDto;
import com.oig.sys.file.service.ITbTemplateFileService;
import com.oig.sys.file.vo.TemplateFileVo;
import com.oig.sys.log.annotation.SysLog;
import com.oig.sys.security.util.SecurityUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@Tag(name = "业务导出模板配置")
@RestController
@RequestMapping("/template")
@Slf4j
@RequiredArgsConstructor
public class TbTemplateFileController {

    private final ITbTemplateFileService templateFileService;


    @PreAuthorize("@pms.hasPermission('/template/add')")
    @SysLog(value="增加导出模板",apiUrl="/template/add")
    @Operation(summary="增加导出模板")
    @PostMapping(value = "add")
    public TemplateFileVo addTemplateFile(@RequestBody @Validated(GroupAdd.class) TemplateFileDto templateFileDto){
        log.info("add template file :{}", templateFileDto);
        SecurityUtil.getDefineUserMain().checkTenantPermission(templateFileDto.getTenantId());
        templateFileDto.setFileUploadId(SecurityUtil.getDefineUserMain().getId());
        templateFileDto.setFileUploadName(SecurityUtil.getDefineUserMain().getRealName());
        templateFileDto.setFileUploadTime(new Date());
        return this.templateFileService.addTemplateFile(templateFileDto);
    }


    @PreAuthorize("@pms.hasPermission('/template/update')")
    @SysLog(value="修改导出模板",apiUrl="/template/update")
    @Operation(summary="修改导出模板")
    @PostMapping(value = "update")
    public TemplateFileVo updateTemplateFile(@RequestBody @Validated(GroupEdit.class) TemplateFileDto templateFileDto){
        log.info("update template file :{}", templateFileDto);
        SecurityUtil.getDefineUserMain().checkTenantPermission(templateFileDto.getTenantId());
        templateFileDto.setFileUploadTime(new Date());
        templateFileDto.setFileUploadId(SecurityUtil.getDefineUserMain().getId());
        templateFileDto.setFileUploadName(SecurityUtil.getDefineUserMain().getRealName());
        return this.templateFileService.updateTemplateFile(templateFileDto);
    }


    @PreAuthorize("@pms.hasPermission('/template/audit')")
    @SysLog(value="导出模板配置复核",apiUrl="/template/audit")
    @Operation(summary = "导出模板配置复核")
    @PostMapping(value = "/audit")
    public TemplateFileVo auditTemplateFile(@RequestParam("id")Integer id,
                                            @RequestParam("resultType")String resultType,
                                            @RequestParam("remark")String remark){
        log.info("audit template file:{}-{}-{}", id, resultType, remark);
        TemplateFileDto templateFileDto = new TemplateFileDto();
        templateFileDto.setId(id);
        templateFileDto.setReqType(resultType);
        templateFileDto.setCheckRemark(remark);
        templateFileDto.setTenantId(SecurityUtil.getDefineUserMain().getTenantId());
        templateFileDto.setCheckId(SecurityUtil.getDefineUserMain().getId());
        templateFileDto.setCheckName(SecurityUtil.getDefineUserMain().getRealName());
        templateFileDto.setCheckTime(new Date());
        return templateFileService.auditTemplateFile(templateFileDto);
    }


    @PreAuthorize("@pms.hasPermission('/template/cancel')")
    @SysLog(value="注销导出模板",apiUrl="/template/cancel")
    @Operation(summary = "注销导出模板")
    @PostMapping(value = "/cancel/{id}")
    public SysResponse<Integer> cancelTemplateFile(@PathVariable("id") Integer id){
        log.info("注销导出模板:{}", id);
        templateFileService.cancelTemplateFile(id);
        return SysResponse.ok(id,"注销导出模板成功");
    }


}
